package com.zhongyu.controller;

import com.zhongyu.pojo.Goods;
import com.zhongyu.pojo.Provider;
import com.zhongyu.service.GoodsService;
import com.zhongyu.vo.GoodsVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/sys/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;


    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize, GoodsVo goodsVo){
        PageResult pageResult=goodsService.searchAll(pageNo,pageSize,goodsVo);
        return ResultVo.success(pageResult);

    }
    @GetMapping("/list")
    public ResultVo list(){
        List<Goods> list=goodsService.searchAll();
//        System.out.println(list);
         return ResultVo.success(list);

    }
    @GetMapping("/dui")
    public ResultVo dui(Integer id){
        Goods goods=goodsService.searchDui(id);
        return ResultVo.success(goods);
    }
    @GetMapping("/lists")
    public ResultVo list(Integer providerId){
        System.out.println("ssssss"+providerId);
        List<Goods> list=goodsService.searchAll(providerId);
//        System.out.println(list);
        return ResultVo.success(list);

    }
    @GetMapping("/cha")
    public ResultVo list(String name){
       Goods list=goodsService.searchCha(name);
        System.out.println("cha"+list);
        return ResultVo.success(list);

    }

    @GetMapping("/faind")
    public void faind(String providerId){
        goodsService.findByDto(providerId);
    }
    @GetMapping("/fan")
    public void fainds(String typeId){
        goodsService.findByDt(typeId);
    }

    @PostMapping("/add")
    public ResultVo add(@RequestBody Goods goods) {
        try {
            goods.setCreationDate(LocalDateTime.now());
            goodsService.add(goods);
            return ResultVo.success("商品添加成功");
        } catch (Exception ex) {
            return ResultVo.failure("商品添加失败", ex);
        }
    }

    @GetMapping("/del")
    public ResultVo del(Integer id) {
        try {
            goodsService.removeById(id);
            return ResultVo.success("删除商品成功!");
        } catch (Exception ex) {
            return ResultVo.failure("删除商品失败！", ex);
        }
    }

    @PostMapping("/update")//post请求要加@RequestBody进行接收
    public ResultVo update(@RequestBody Goods goods) {
        try {
            goodsService.update(goods);
            return ResultVo.success("修改商品成功");
        } catch (Exception ex) {
            return ResultVo.failure("修改商品失败", ex);
        }

    }




}
